<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>register</title>
    <link rel="icon" href="../images/logo.ico">
    <style>
        body {
            overflow: hidden;
            background-color: #F2D9E6;
        }

        form {
            width: 400px;
            margin: 200px auto;
            border: 1px solid #000;
            padding: 20px;
            text-align: center;
        }

        form div {
            margin: 20px auto;
        }

        form label {
            width: 100px;
            display: inline-block;
            text-align: right;
        }

        .btn {
            padding: 5px 25px;
        }

        h2>a {
            font-size: 14px;
            /* vertical-align: bottom; */
        }
    </style>
</head>

<body>
    <form action="#" id="register">
        <h2>注册
            <a href="./login.html">登录</a>
            <a href="./delet.html">注销</a>
        </h2>
        <div>
            <label for="username">
                账号:
            </label>
            <input type="text" id="username" name="username">
        </div>
        <div>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password">
        </div>
        <div>
            <label for="rePassword">确认密码:</label>
            <input type="password" id="rePassword" name="rePassword">
        </div>
        <div>
            <label for="act">
                激活码：
            </label>
            <input type="text" name="act" id="act" value="">
        </div>
        <div>
            <input class="btn" type="submit" value="注册" id="register-btn">
            <input class="btn" type="reset" value="重置">
        </div>
    </form>
    <script src="../js/jquery-3.3.1.js"></script>
    <script>
        var submitBtn = document.getElementById('register-btn');
        submitBtn.onclick = function (e) {
            e.preventDefault();
            var form = document.getElementById('register');
            var username = form.username.value;
            var password = form.password.value;
            var rePassword = form.rePassword.value;
            var act = form.act.value;
            if (username && password && rePassword && act) {
                if (password == rePassword) {
                    var data = {
                        username:username,
                        password:password,
                        act:act
                    }
                    $.ajax({
                        type: "POST",
                        data:data,
                        url: "../php/register.php",
                        success: function (res) {
                            if (res.status){
                                alert("注册成功");
                                location.href="login.html"
                            }else {
                                    alert(res.msg);
                            }
                        },
                        error: function (res) {
                            console.warn(res);
                            alert("请求失败！");
                        }
                    })
                } else {
                    alert("密码前后不一致");
                }
            } else {
                alert("请完善信息！");
            }
        }
    </script>
</body>

</html>